package com.csst.business.action;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.id.UUIDGenerator;

import com.csst.business.model.Award;
import com.csst.business.model.AwardResult;
import com.csst.business.model.Awresult;
import com.csst.business.model.TWuser;
import com.csst.business.service.IAwardService;
import com.csst.popular.api.SnsAPI;
import com.csst.popular.bean.SnsToken;
import com.csst.popular.bean.User;
import com.csst.services.Thread.TokenThread;
import com.opensymphony.xwork2.ActionSupport;


/**
 * @author handq
 *
 */
@Entity
@SuppressWarnings("serial")
public class ScrathAction extends ActionSupport {
	private IAwardService awardService;
	private Award scrath;
	private AwardResult aresult;
	
	/**
	 * 挂奖的业务逻辑
	 * @return
	 */		
	public String getResult(){
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser) request.getSession().getAttribute("tuser");
		aresult = new AwardResult();
		if(tuser!=null){
			int count = awardService.getScrathCount(tuser.getWOpenid());
			if(count >=3){
				aresult.setSuccess(false);
				aresult.setAngle("1000");
				aresult.setMsg("对不起，您今天的三次机会都已经用完了，明天再来吧！");
			}else{
				Object obj[] = lottery();//抽奖后返回角度和奖品等级
				scrath = new Award();
				scrath.setAddTime(new Date());
				scrath.setUserOpenId(tuser.getWOpenid());
				scrath.setPrizetype(String.valueOf(obj[0]));
				scrath.setAtype("scrath");
				awardService.saveAwardData(scrath);
				aresult.setSuccess(true);
				aresult.setAngle(String.valueOf(obj[0]));
				aresult.setPrize(String.valueOf(obj[1]));
				aresult.setMsg(String.valueOf(obj[1]));
				 String uu = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
				aresult.setSncode(uu.substring(0, 12)); 
				System.out.println(String.valueOf(obj[0])+"=="+String.valueOf(obj[1])+"=="+String.valueOf(obj[1])+"=="+uu.substring(0, 12));
			}
			
		}else{
			aresult.setSuccess(false);
			aresult.setMsg("只有我们的微信绑定会员才可以抽奖哟！您可以在微信会员中心绑定.");
		}
		return SUCCESS;
	}
	
	public boolean saveResultAjax(String prized,String scno,String phone){
		HttpServletRequest request = ServletActionContext.getRequest();
		TWuser tuser = (TWuser) request.getSession().getAttribute("tuser");
		if(tuser!=null){
			Awresult awresult = new Awresult();
			awresult.setAddTime(new Date());
			awresult.setPhone(phone);
			awresult.setPrized(prized);
			awresult.setScno(scno);
			awresult.setUserName(tuser.getWNickname());
			awresult.setUserOpenId(tuser.getWOpenid());
			awresult.setPtype("刮刮卡");
			awresult.setPstate("未兑奖");
			awardService.saveAwresultData(awresult);
			return true;
		}else{
			return false;
		}
	}
	
	
	// 抽奖并返回角度和奖项
    public Object[] lottery()
    {
        Object[][] lott = getLunckArray();
        // 概率数组
        Integer obj[] = new Integer[lott.length];
        for (int i = 0; i < lott.length; i++)
        {
            obj[i] = (Integer) lott[i][2];
        }
        Integer prizeId = getRand(obj); // 根据概率获取奖项id
        String msg = (String) lott[prizeId][1];// 提示信息
        return new Object[]
        { prizeId, msg };
    }

    protected static Object[][] getLunckArray()
    {

        Object luckArr[][] = new Object[][]
        {
        { 0, "一等奖", 1 },
        { 1, "二等奖", 2 },
        { 2, "三等奖", 3 },
        { 3, "参与奖", 10 },
        { 4, "谢谢惠顾", 20 },
        { 5, "谢谢惠顾", 20 },
        { 6, "谢谢惠顾", 50 } };

        return luckArr;
    }

    protected static Integer getRand(Integer obj[])
    {
        Integer result = null;
        int sum = 0;// 概率数组的总概率精度
        for (int i = 0; i < obj.length; i++)
        {
            sum += obj[i];
        }
        for (int i = 0; i < obj.length; i++)
        {
            int randomNum = new Random().nextInt(sum);// 随即生成1到sum的整数
            if (randomNum < obj[i])
            {// 中奖
                result = i;
                break;
            }
            else
            {
                sum -= obj[i];
            }
        }
        return result;
    }

	public IAwardService getAwardService() {
		return awardService;
	}

	public void setAwardService(IAwardService awardService) {
		this.awardService = awardService;
	}

	public Award getScrath() {
		return scrath;
	}

	public void setScrath(Award scrath) {
		this.scrath = scrath;
	}

	public AwardResult getAresult() {
		return aresult;
	}

	public void setAresult(AwardResult aresult) {
		this.aresult = aresult;
	}

	//getter&&setter
	
}
